package com.ysy.logistics.entities.system.vo;

import cn.hutool.core.collection.CollUtil;
import com.ysy.logistics.entities.system.MenuNode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/20 11:54
 **/
@Data
@Schema(name = "RoleMenuTreeVO",description = "角色菜单树")
public class RoleMenuTreeVO {
    private String id;
    private String pid;
    private String name;
    private Boolean checked = false;
    private List<RoleMenuTreeVO> children;

    public static List<RoleMenuTreeVO> convertToRoleMenuTreeVO(List<MenuNode> menuNode,List<Long> checkedMenuIds) {
        if (CollUtil.isEmpty(menuNode)){
            return null;
        }
        List<RoleMenuTreeVO> result = new ArrayList<>();

        for (int i = 0; i < menuNode.size(); i++) {
            RoleMenuTreeVO roleMenuTreeVO = new RoleMenuTreeVO();
            roleMenuTreeVO.setId(menuNode.get(i).getId().toString());
            roleMenuTreeVO.setPid(menuNode.get(i).getParentId().toString());
            roleMenuTreeVO.setName(menuNode.get(i).getName());
            if (CollUtil.isNotEmpty(checkedMenuIds)){
                String id2 = menuNode.get(i).getId().toString();
                for (Long id : checkedMenuIds) {
                    if (id.equals(id2)){
                        roleMenuTreeVO.setChecked(true);
                    }
                }
            }
            if (CollUtil.isNotEmpty(menuNode.get(i).getChildren())){
                roleMenuTreeVO.setChildren(convertToRoleMenuTreeVO(menuNode.get(i).getChildren(), checkedMenuIds));
            }
            result.add(roleMenuTreeVO);
        }

        return result;

    }

    private void addChildren(RoleMenuTreeVO roleMenuTreeVO1){
        this.children.add(roleMenuTreeVO1);
    }
}
